package com.example.wechat;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Fragment WeChatFragment;
    private Fragment friendFragment;
    private Fragment circleFragment;
    private Fragment configFragment;
    private FragmentManager fm;

    private ImageView weixinButton;
    private ImageView friendButton;
    private ImageView findButton;
    private ImageView SettingButton;

    private TextView weixinText;
    private TextView friendText;
    private TextView findText;
    private TextView SettingText;

    private LinearLayout tab01,tab02,tab03,tab04;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WeChatFragment=new WeChatFragment();
        circleFragment=new circleFragment();
        friendFragment=new friendFragment();
        configFragment=new configFragment();

        tab01=findViewById(R.id.tab01);
        tab02=findViewById(R.id.tab02);
        tab03=findViewById(R.id.tab03);
        tab04=findViewById(R.id.tab04);

        weixinButton=findViewById(R.id.weixinButton);
        friendButton=findViewById(R.id.friendButton);
        findButton=findViewById(R.id.findButton);
        SettingButton=findViewById(R.id.SettingButton);

        weixinText=findViewById(R.id.weixinText);
        friendText=findViewById(R.id.friendText);
        findText=findViewById(R.id.findText);
        SettingText=findViewById(R.id.SettingText);

        fm=getSupportFragmentManager();
        initalfragment();


        tab01.setOnClickListener(this);
        tab02.setOnClickListener(this);
        tab03.setOnClickListener(this);
        tab04.setOnClickListener(this);

    }

    private void initalfragment() {
        FragmentTransaction transaction=fm.beginTransaction();
        transaction.add(R.id.content,WeChatFragment);
        transaction.add(R.id.content,friendFragment);
        transaction.add(R.id.content,circleFragment);
        transaction.add(R.id.content,configFragment);
        Hide(transaction);
        transaction.show(WeChatFragment);
        transaction.commit();
    }


    @Override
    public void onClick(View view) {
        resetBtn();
        switch (view.getId()){
            case R.id.tab01:
                show(1);
                break;
            case R.id.tab02:
                show(2);
                break;
            case R.id.tab03:
                show(3);
                break;
            case R.id.tab04:
                show(4);
                break;
            default:
                break;
        }
    }

    private void show(int i) {
        FragmentTransaction transaction=fm.beginTransaction();
        Hide(transaction);
        switch (i){
            case 1:transaction.show(WeChatFragment);weixinButton.setImageResource(R.drawable.message_pressed);break;
            case 2:transaction.show(friendFragment);friendButton.setImageResource(R.drawable.friend_pressed);break;
            case 3:transaction.show(circleFragment);findButton.setImageResource(R.drawable.circle_pressed);break;
            case 4:transaction.show(configFragment);SettingButton.setImageResource(R.drawable.setting_pressed);break;
            default:
                break;
        }
        transaction.commit();

    }

    private void Hide(FragmentTransaction transaction) {
        transaction.hide(WeChatFragment);
        transaction.hide(friendFragment);
        transaction.hide(circleFragment);
        transaction.hide(configFragment);
    }

    private void resetBtn(){
        weixinButton.setImageResource(R.drawable.message);
        friendButton.setImageResource(R.drawable.friend);
        findButton.setImageResource(R.drawable.circle);
        SettingButton.setImageResource(R.drawable.setting);
    }
}
